package com.ruoyi.learn.java.thread;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class YieldAndJoin {

    public static void main(String[] args) throws InterruptedException {
        // yield() 示例
        Thread producer = new Thread(() -> {
            while (true) {
                // 生产数据
                Thread.yield(); // 让出CPU给消费者
                log.info("Producing data...");
                Thread.yield(); // 让出CPU给消费者
                log.info("Produced data.");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        producer.start();
// join() 示例
        Thread worker = new Thread(() -> {
            // 执行任务
            log.info("Worker started");
        });
        worker.start();
        worker.join(); // 主线程等待worker完成
        System.out.println("Worker finished");
    }

}
